<style lang="scss" scoped>
@import '../../style/mixin-rem.scss';
.book-details-to-reader-btn {
  .reader-btn-group {
    z-index: 9999;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    display: flex;
    justify-content: space-between;
    .btn-once {
      width: 50%;
      font-size: 28px;
      text-align: center;
      padding: rem(36px) 0;
      background-color: #f2f2f2;
      color: #ababab;
    }
    .btn-add {
      width: 50%;
      font-size: 28px;
      text-align: center;
      padding: rem(36px) 0;
      background-color: #ff3c31;
      color: #fff;
    }
  }
  .reader-btn {
    z-index: 9999;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    font-size: 28px;
    text-align: center;
    padding: rem(36px) 0;
    background-color: #ff3c31;
    color: #fff;
  }
}
</style>

<template>
  <div class="book-details-to-reader-btn">
    <!-- 阅读按钮组 -->
    <div class="reader-btn-group" v-if="btnHasShow">
      <div class="btn-once">立即阅读</div>
      <div class="btn-add" @click="addBookCaseItem">阅读并加入书架</div>
    </div>
    <!-- 继续阅读 -->
    <div class="reader-btn" v-if="!btnHasShow" @click="toReaderPage">
      继续阅读
    </div>
  </div>
</template>

<script>
export default {
  name: 'bookDetailsToReaderBtn',
  data () {
    return {
      btnHasShow: true,
      id: '',
      chapterId: ''
    }
  },
  created () {
    this.initBtn()
  },
  methods: {
    initBtn () {
      let bookCase = JSON.parse(localStorage.getItem('bookCaseContent'))
      let id = this.$route.params.id.replace(/info-/g, '')
      if (bookCase.length !== 0) {
        for (let i = 0; i < bookCase.length; i++) {
          if (bookCase[i].id === id) {
            this.id = bookCase[i].id
            this.chapterId = bookCase[i].chapterId
            this.btnHasShow = false
          }
        }
      }
    },
    addBookCaseItem () {
      this.$store.commit('bookCase/addItem', this.$store.state.bookDetails.info)
      this.id = this.$store.state.bookDetails.info.id
      this.chapterId = this.$store.state.bookDetails.info.firstChapterId
      this.toReaderPage()
    },
    toReaderPage () {
      this.$router.push({ name: 'bookReader', params: { id: `wapbook-${this.id}-${this.chapterId}` } })
    }
  }
}
</script>
